package org.jeecg.modules.flowable.util;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 7/19/22 8:29 AM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
public class ObjectUtil {
    /**
     * 判断表单数据类型
     *
     * @param argObject
     * @return
     */
    public static String getFormDataType(Object argObject) {
        if (argObject == null)
            return "null";
        String dataType = "unknown";
        if (argObject instanceof Integer)
            dataType = "integer";
        else if (argObject instanceof String)
            dataType = "string";
        else if (argObject instanceof Double)
            dataType = "double";
        else if (argObject instanceof BigDecimal)
            dataType = "bigdecimal";
        else if (argObject instanceof Float)
            dataType = "float";
        else if (argObject instanceof Long)
            dataType = "long";
        else if (argObject instanceof Boolean)
            dataType = "boolean";
        else if (argObject instanceof ArrayList) {
            dataType = "list";
        } else
            dataType = "object";
        return dataType;
    }

    /**
     * 表单数据跟据数据库中记录的类型转成OBJECT对像，返给前端
     *
     * @param argString
     * @param argDataType
     * @return
     */
    public static Object parseObject(String argString, String argDataType) {
        if (argDataType == null || argDataType.toString().trim().equals("null") || argDataType.toString().trim().equals("object") || argDataType.toString().trim().equals("unknown")) {
            return argString;
        } else if (argDataType.toString().equals("integer")) {
            return Integer.parseInt(argString);
        } else if (argDataType.toString().equals("string")) {
            return argString;
        } else if (argDataType.toString().equals("double")) {
            return Double.parseDouble(argString);
        } else if (argDataType.toString().equals("bigdecimal")) {
            return new BigDecimal(argString);
        } else if (argDataType.toString().equals("float")) {
            return Float.parseFloat(argString);
        } else if (argDataType.toString().equals("long")) {
            return Long.parseLong(argString);
        } else if (argDataType.toString().equals("boolean")) {
            return Boolean.valueOf(argString);
        } else if (argDataType.toString().equals("list")) {
            Object parse = JSONObject.parse(argString);
            return parse;
        } else if (argDataType.toString().equals("object")) {
            Object parse = JSONObject.parse(argString);
            return parse;
        } else {
            return argString;
        }

    }
}